package com.vedana.bankmobile.shared.protobuf.serializers;

import java.util.ArrayList;
import java.util.List;

import com.vedana.bankmobile.shared.Customer;
import com.vedana.bankmobile.shared.protobuf.BankProto.CustomerMsg;

/**
 * Serialize Customer domain objects to protobuf messages
 * 
 * @author Didier MOISE
 *
 */
public class CustomerSerializer {
	/**
	 * Read Customer POJO from protobuf message
	 * @param msg Protobuf message
	 * @return Customer POJO
	 */
	public static Customer readFromMsg(CustomerMsg msg) {
		Customer customer = new Customer();

		customer.setId(msg.getId());
		customer.setLabel(msg.getLabel());

		return customer;
	}

	/**
	 * Write Customer POJO to protobuf message
	 * @param customer Customer POJO
	 * @return Protobuf message
	 */
	public static CustomerMsg writeToMsg(Customer customer) {
		CustomerMsg msg = CustomerMsg.newBuilder().setId(customer.getId())
				.setLabel(customer.getLabel()).build();
		return msg;
	}

	/**
	 * Read List of customer POJO from protobuf message
	 * @param msg Protobuf message
	 * @return Customer POJO list
	 */
	public static List<Customer> readListFromMsg(List<CustomerMsg> msg) {
		List<Customer> customers = new ArrayList<Customer>();
		for (CustomerMsg m : msg) {
			customers.add(readFromMsg(m));
		}
		return customers;
	}

	/**
	 * Write Customer POJO list to protobuf message
	 * @param customers Customer POJO list
	 * @return Protobuf message
	 */
	public static List<CustomerMsg> writeListToMsg(List<Customer> customers) {
		List<CustomerMsg> msg = new ArrayList<CustomerMsg>();
		for (Customer customer : customers) {
			msg.add(writeToMsg(customer));
		}
		return msg;
	}
}
